<?php

require_once(PATH_CLASS.'widget.class.php');
require_once(PATH_CLASS.'equipe.class.php');
require_once(PATH_CLASS.'session.class.php');

class form extends widget{
  
  const CLASS_NAME  = 'form';
  const CLASS_TYPE  = '';
  
  public $type      = '';

  public function getDefaults() {
    $ret = parent::getDefaults();
    $ret = array_merge(array(
       'id'         => null
      ,'pars'       => array()
      ,'class'      => array()
      ,'style'      => array()
    ),$ret);
    
    return $ret;
  }
  
  public function isAuthorized() {
    $ret = parent::isAuthorized();
    return $ret;
  }
  
  public function __construct($params) {
    parent::__construct($params);
    
    if(isset($params['type'])){
      $this->addWidgetCss($params['type'].'.css');
      $this->addWidgetJs($params['type'].'.js');
    }
    
    $file = PATH_CLASS.'widgets/form/'.$params['type'].'.class.php';
    if(file($file)){
      require_once $file;
      $class = new $params['type']($params);
      $this->params = array_merge($this->params,$class->params);
    }
    
    if(isset($this) && isset($this->params) && isset($this->params['type'])){
      $this->type = $this->params['type'];
    }

    if(!$this->isAuthorized()){
      security::headerForbidden();
      throw new Exception("Non pas le droit");
    }
    
  }
  
  protected function _get_called_class() { 
    $ret = self::CLASS_NAME;
    
//    if(isset(self::$type) && !empty(self::$type)){
//      $ret .='/'.self::$type;
//    }
      
    return $ret;  
  }

  public function _render() {
    $pars   = $this->params;
    $params = array();
    $params = array_merge($params,$pars);
    
    $fileTb = explode('.class.php', __FILE__);
    
    if(isset($pars['type']) && count($fileTb)>1){
      $file = $fileTb[0].'/'.$pars['type'].'.class.php';
      $layout = $this->renderTplFromFile($file, $params);
      $widgetName = $this->_get_called_class().'/'.$pars['type'];
      self::setLoaded($widgetName);
    } else {
      $layout='';
    }

    return $layout;
  }
}

